import { ref } from 'vue'
import { adapter, request } from '@/request'
import { createCampaignByPost } from '@/core/api/promotion'
import is from '@/utils/is'
import { message } from 'ant-design-vue'
import type { Order } from '@/core/interfaces/order'
import type { CustomerPack } from '@/core/interfaces/base'
import type { RequestWrapper } from '@/core/interfaces/common'

interface Params {
  orderId: Order['id']
  orderNo: Order['orderNo']
  customerId: CustomerPack['customerId']
}

export default function createCampaign() {
  const service = adapter<RequestWrapper<any>>(request)
  const loading = ref(false)

  const fetch = (data: Params) => {
    const { url, method } = createCampaignByPost()
    loading.value = true

    return service({
      config: {
        url,
        method,
        data
      },
      error: (err) => {
        if (is.error(err)) {
          message.error(err.message)
        } else {
          message.error('请求失败，请稍候再试...')
        }
      },
      done: () => {
        loading.value = false
      }
    })
  }

  return [null, fetch, loading] as const
}
